<?php
require_once('../lib/standard.php');
require_once('../lib/notes.php');
$pineza = new Globals(FALSE);
if (!session_set('admin'))
	telos('Administrator denied!');

unset($_SESSION['login']);
@mysqli_query($pineza->db, "DROP DATABASE IF EXISTS `{$pineza->db_name}`") ||
	die("Drop database failed for `{$pineza->db_name}`: " . mysqli_error($pineza->db));
@mysqli_query($pineza->db, "CREATE DATABASE `{$pineza->db_name}`") ||
	die("Create database failed for `{$pineza->db_name}`: " . mysqli_error($pineza->db));
@mysqli_set_charset($pineza->db, 'UTF8') ||
	die("Failed to set character set for `{$pineza->db_name}`: " . mysqli_error($pineza->db));
@mysqli_select_db($pineza->db, $pineza->db_name) ||
	die('Failed to select database "' . $pineza->db_name .
		'" for `{$pineza->db_name}`: ' . mysqli_error($pineza->db));

neos_pinakas('user');
neos_pinakas('board');
neos_pinakas('note');
load_data();
$pineza->close_database();
print AJAX_SEP;

function neos_pinakas($table) {
	global $pineza;
	$query = @file_get_contents("schema/{$table}.sql");
	if (!$query) {
		telos("{$table}: failed to open schema");
	}

	if (!@mysqli_query($pineza->db, $query)) {
		die("Create table failed for `{$table}`: " . mysqli_error($pineza->db));
		$pineza->close_database();
	}
}

function load_data() {
	global $pineza;
	$result = @mysqli_query($pineza->db,
		"INSERT INTO `user` (`login`, `name`, `password`) " .
		"VALUES ('', 'System Account', '{$pineza->admpass}')");
	if ((!$result) ||(mysqli_affected_rows($pineza->db) != 1)) {
		telos('load_data: failed (' . mysqli_error($pineza->db) . ')');
	}

	insert_board('', '', 'Draft board');
	insert_board('', 'GREEN', 'Green board');
	insert_board('', 'INDIAN', 'Indian board');
	insert_board('', 'NEUTRAL', 'Neutral board');
	insert_board('', 'TEAL', 'Teal board');
	insert_board('', 'PAPER', 'Paper board');
	insert_board('', 'LILA', 'Violette board');
	insert_board('', 'RED', 'Red board');
	insert_board('', 'TRASH', 'Trash');

	$font = 'Comic Sans MS, cursive, serif';
	new_note('', '', "<h1>Welcome!</h1>Too many jobs to be done? " .
		"Can't remember what you have to do? You are at the right place!",
		$font, 'B8DB4D', FALSE, 'HIGH');
	new_note('', '', "<h1>Divide et impera!</h1>A variety of boards is at your service. " .
		"It's in your choice to stick your notes all together, " .
		"or organize them in various boards!",
		$font, 'FFFF33');
	new_note('', '', "<h1>Color your jobs!</h1>You can choose colors from " .
		"the rich RGB color suit. It's all up to you!",
		$font, 'FF66A3');
	new_note('', '', "<h1>Signup now!</h1><a href=\"#\" onclick=\"parent.location='" .
		$pineza->server . "account/index.php'; return false;\" title=\"Signup\">" .
		"Signup</a> now and start sticking your notes right away. It's simple!",
		$font, 'FFFFA3');
}
?>
